
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>录音</title>
    </head>
    <body  background="123.jpg">
        <div>
            <audio autoplay></audio>
            <input onclick="startRecording()" type="button" value="录音" />
            <input onclick="stopRecording()" type="button" value="停止" />
            <input onclick="playRecording()" type="button" value="播放" />
            <input onclick="uploadAudio()" type="button" value="提交" />
            <br />
            <div id="recordingslist"></div>
        </div>
        <script type="text/javascript" src="js/HZRecorder.js"></script>
        <script>
            var recorder;
            var audio = document.querySelector('audio');
            function startRecording() {
                HZRecorder.get(function(rec) {
                    recorder = rec;
                    recorder.start();
                }, {
                    sampleBits: 16,
                    sampleRate: 16000
                });
            }
            function stopRecording() {
                recorder.stop();
                var blob = recorder.getBlob();
                var url = URL.createObjectURL(blob);
                var div = document.createElement('div');
                var au = document.createElement('audio');
                var hf = document.createElement('a');
                au.controls = true;
                au.src = url;
                hf.href = url;
                hf.download = new Date().toISOString() + '.wav';
                hf.innerHTML = hf.download;
                div.appendChild(au);
                div.appendChild(hf);
                recordingslist.appendChild(div);
            }
            function playRecording() {
                recorder.play(audio);
            }
            function uploadAudio() {
                recorder.upload("Handler1.ashx", function(state, e) {
                    switch(state) {
                        case 'uploading':
                            //var percentComplete = Math.round(e.loaded * 100 / e.total) + '%';
                            break;
                        case 'ok':
                            //alert(e.target.responseText);
                            alert("上传成功");
                            break;
                        case 'error':
                            alert("上传失败");
                            break;
                        case 'cancel':
                            alert("上传被取消");
                            break;
                    }
                });
            }
        </script>
    </body>
</html>
<!DOCTYPE html>
<html>

<head>
    <title>录频提问</title>
    <script src="fileSaver.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</head>
<style>
    body{
        background-color:#EFEDEF;
    }
</style>
<body  background="123.jpg">
    <article style="border:1px solid white;width:400px;height:400px;margin:0 auto;background-color:white;">

        <section class="experiment" style="width:320px; height:240px;border:1px solid green; margin:50px auto;">
            <div id="videos-container" style="width:320px; height:240px;">
            </div>
        </section>
        <section class="experiment" style="text-align:center;border:none; margin-top:20px;">
            <button id="openCamera">打开摄像头</button>
            <button id="start-recording" disabled>开始录制</button>
            <button id="save-recording" disabled>保存</button>
            <!--<a href="javascript:void(0)" οnclick="send()">发送</a>-->
        </section>
 </article>
<script>
            var mediaStream;
            var recorderFile;
            var stopRecordCallback;
            var openBtn = document.getElementById("openCamera");
            var startBtn = document.getElementById("start-recording");
            var saveBtn = document.getElementById("save-recording");
            openBtn.onclick = function() {
                this.disabled = true;
                startBtn.disabled=false;
                openCamera();
            };

            startBtn.onclick = function() {
                this.disabled = true;
                startRecord();
            };

            saveBtn.onclick = function() {
                saver();

                // alert('Drop WebM file on Chrome or Firefox. Both can play entire file. VLC player or other players may not work.');
            };

            var mediaRecorder;
            var videosContainer = document.getElementById('videos-container');

            function openCamera(){
                var len = videosContainer.childNodes.length;
                for(var i=0;i<len;i++){
                    videosContainer.removeChild(videosContainer.childNodes[i]);
                }

                var video = document.createElement('video');

                var videoWidth =320;
                var videoHeight = 240;

                video.controls = false;
                video.muted = true;
                video.width = videoWidth;
                video.height = videoHeight;
                MediaUtils.getUserMedia(true, false, function (err, stream) {
                    if (err) {
                        throw err;
                    } else {
                        // 通过 MediaRecorder 记录获取到的媒体流
                        console.log();
                        mediaRecorder = new MediaRecorder(stream);
                        mediaStream = stream;
                        var chunks = [], startTime = 0;
                        video.srcObject = stream;
                        video.play();

                        videosContainer.appendChild(video);
                        mediaRecorder.ondataavailable = function(e) {
                            mediaRecorder.blobs.push(e.data);
                            chunks.push(e.data);
                        };
                        mediaRecorder.blobs = [];

                        mediaRecorder.onstop = function (e) {
                            recorderFile = new Blob(chunks, { 'type' : mediaRecorder.mimeType });
                            chunks = [];
                            if (null != stopRecordCallback) {
                                stopRecordCallback();
                            }
                        };
                }
            });
            }

            // 停止录制
            function stopRecord(callback) {
                stopRecordCallback = callback;
                // 终止录制器
                mediaRecorder.stop();
                // 关闭媒体流
                MediaUtils.closeStream(mediaStream);
            }

            var MediaUtils = {
                /**
                * 获取用户媒体设备(处理兼容的问题)
                * @param videoEnable {boolean} - 是否启用摄像头
                * @param audioEnable {boolean} - 是否启用麦克风
                * @param callback {Function} - 处理回调
                */
                getUserMedia: function (videoEnable, audioEnable, callback) {
                    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia
                    || navigator.msGetUserMedia || window.getUserMedia;
                    var constraints = {video: videoEnable, audio: audioEnable};
                    if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
                        navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
                        callback(false, stream);
                    })['catch'](function(err) {
                        callback(err);
                    });
                    } else if (navigator.getUserMedia) {
                        navigator.getUserMedia(constraints, function (stream) {
                        callback(false, stream);
                    }, function (err) {
                        callback(err);
                    });
                } else {
                    callback(new Error('Not support userMedia'));
                }
            },

                /**
                * 关闭媒体流
                * @param stream {MediaStream} - 需要关闭的流
                */
                closeStream: function (stream) {
                    if (typeof stream.stop === 'function') {
                        stream.stop();
                    }
                    else {
                        let trackList = [stream.getAudioTracks(), stream.getVideoTracks()];

                        for (let i = 0; i < trackList.length; i++) {
                            let tracks = trackList[i];
                            if (tracks && tracks.length > 0) {
                                for (let j = 0; j < tracks.length; j++) {
                                    let track = tracks[j];
                                    if (typeof track.stop === 'function') {
                                        track.stop();
                                    }
                                }
                            }
                        }
                    }
                }
            }; 

function startRecord() {
    mediaRecorder.start();
    setTimeout(function(){
        // 结束
        stopRecord(function() {
            alert("录制成功!");
            openBtn.disabled=false;
            saveBtn.disabled=false;
            //send();
        });
    }, 5000);
}

function saver(){
    var file = new File([recorderFile], 'msr-' + (new Date).toISOString().replace(/:|\./g, '-') + '.mp4', {
        type: 'video/mp4'
    });
    saveAs(file);
}

function send(){
    var file = new File([recorderFile], 'msr-' + (new Date).toISOString().replace(/:|\./g, '-') + '.mp4', {
        type: 'video/mp4'
    });
    var data = new FormData();
    data.append("username", "test");
    data.append("userfile", file);

    var req = new XMLHttpRequest();
    req.open("POST", "com.spinsoft.bip.frame.utils.image.saveMp4.biz.ext");
    req.send(data);
}

</script>

</body>

</html>
<!DOCTYPE html>

<meta charset="utf-8">
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>交互式录播器</title>
    <style type="text/css">

        .ack {
            display: inline;
            position: absolute;
            top: 85px;
            margin: 0 0 0 5px;
            background-color:#16A3DF;
            border: 1px solid white;
        }

        .ack h3 {
            text-align: center;
            color: white;
        }

        .massge {
            height: 200px;
            width: 300px;
            color: yellow;
            overflow-y: auto;
        }

        .massge audio {
            height: 20px;
            width: 250px;
        }
    </style>
</head>

<body>
    <div class="allComponents">
       
            <div class="ack">
                <h3>向老师提问</h3>
                <div class="massge" id="massge">

                </div>
                <div class="weben">
                    <form name="input">
                        <input type="text" name="qs" placeholder=" 请输入你的问题" id="qst">
                        <input type="button" value="提交" onclick="send()"><br>
                        <p></p>

                    </form>
                </div>
            </div>
        </div>

</body>
<script>
    // 录音
    var flage = true
    var luyin = function () {
        recOpen(
            function () {
                {
                    rec.start();
                    flage = false
                }
            }
        )
    }
    var luyinst = function () {
        recStop();
    }
    // 文字
    let left = false;
    var talk = document.getElementById('massge');
    let send = function () {
        let node = document.createElement('p');
        let div = document.createElement('div');
        div.className = 'wall';
        let textNode = document.createTextNode(document.getElementById('qst').value);
        node.appendChild(textNode);
        node.className = 'ltalk';
        div.appendChild(node);
        talk.appendChild(div);
        document.getElementById('qst').value = "";

    }
</script>
</html>



